//
//  HomeFlowNewFreeProCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/10/22.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "HomeFlowNewFreeProCell.h"
#import "HomeFlowProductChildCell.h"
#import "JJCardSliderFlowLayout.h"

@interface HomeFlowNewFreeProCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    
    CGSize _cellSize;
    UIEdgeInsets _collectionEdge;
}

@end

@implementation HomeFlowNewFreeProCell

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self layout];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccessAction) name:@"BA_LoginSuccess" object:nil];

}

- (void)loginSuccessAction {
    [self.collectionview reloadData];
}

+ (CGFloat)calculateHeight {
    
    CGFloat collectionHeight = ((TTScreenWidth-20)/2.335 + 20);
    return collectionHeight;
}

- (void)layout {
    
    CGFloat distance = 5;
    CGFloat height = 0.2633*(TTScreenWidth-20);
    CGFloat width = (TTScreenWidth-70)-(0.2028*(TTScreenWidth-20));

    _cellSize =  CGSizeMake(width, height);
    _collectionEdge = UIEdgeInsetsMake(distance, distance, distance, distance);

    JJCardSliderFlowLayout *flowLayout = [[JJCardSliderFlowLayout alloc] init];

    self.collectionview.collectionViewLayout = flowLayout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.pagingEnabled = YES;
    self.collectionview.backgroundColor = [UIColor clearColor];
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.showsVerticalScrollIndicator = NO;
    
    [self.collectionview registerNib:[UINib nibWithNibName:@"HomeFlowProductChildCell" bundle:nil] forCellWithReuseIdentifier:@"HomeFlowProductChildCellID"];
}

- (void)fillCellWithModel:(NSMutableArray <CommonPackageModel *>*)dataArray {

    self.dataArray = dataArray;
    [self.collectionview reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeFlowProductChildCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlowProductChildCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return _cellSize;
}

//设置cell的位置
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section {

    return _collectionEdge;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.HomeFlowNewFreeProCellBlock) {
        self.HomeFlowNewFreeProCellBlock();
    }
}

@end
